<template>
  <span class="el-dropdown-link" v-if="!loginStatus">
    <div
      @click="$router.push({ path: '/login', query: { to: '/' } })"
      class="el-dropdown-link-item"
    >
      点击登录
    </div>
  </span>
  <el-tooltip v-else class="item" effect="light" width="120" placement="bottom">
    <span class="el-dropdown-link" v-if="userInfo.avatarUrl != null">
      <div class="el-dropdown-link-item">
        <img :src="baseApi + userInfo.avatarUrl" class="avatar" />
        <span style="margin-left: 5px" v-if="showText">{{
          userInfo.nickname
        }}</span>
        <i
          class="el-icon-arrow-down el-icon--right"
          style="margin-right: 5px"
        ></i>
      </div>
    </span>

    <template #content>
      <div v-if="loginStatus" class="info-msg">
        <div @click="$router.push('/person')">个人信息</div>
      </div>
      <div v-if="loginStatus" class="info-msg">
        <div @click="logout">退出</div>
      </div>
    </template>
  </el-tooltip>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ref } from "vue";

const store = useStore();
const router = useRouter();

// Props
const props = defineProps({
  showText: {
    type: Boolean,
    default: true,
  },
  userInfo: {
    type: Object,
    default: () => ({
      avatarUrl: null,
    }),
  },
  loginStatus: {
    type: Boolean,
  },
  role: {
    type: String,
  },
});

// Data
const baseApi = ref<string>(store.state.baseApi);

// Methods
const logout = () => {
  localStorage.removeItem("user");
  router.go(0);
  store.state.message.success("退出成功");
};
</script>

<style scoped lang="less">
a {
  text-decoration: none;
}

.navagation {
  width: 100%;
  height: 60px;
  line-height: 60px;
  overflow: hidden;
}

.avatar {
  width: 45px;
  height: 45px;
  border-radius: 100%;
  position: relative;
}

.menu-item {
  padding-left: 50px;
  padding-right: 50px;
  background-color: var(--header-nav) !important;
}

.el-menu {
  background-color: var(--header-nav) !important;
}

.el-menu-item:not(.is-disabled):hover {
  color: var(--nav-text) !important;
  background-color: var(--header-nav) !important;
  background-color: var(--hover-bg) !important;
}

.el-menu-item.is-active {
  color: var(--nav-text) !important;
  border-bottom: solid 2px var(--nav-text) !important;
  background-color: var(--hover-bg) !important;
}

.good-menu a span {
  color: var(--classify-text);
}

.good-menu a span:hover {
  color: var(--classify-text-hover) !important;
}

.el-dropdown {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.el-dropdown-link-item {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  margin: 0 20px;
}

.info-msg {
  height: 30px;
  width: 100px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  &:hover {
    background-color: rgb(213, 247, 251);
  }
}
</style>
